<template>
  <div class="toast">
        <i :class="getClass"></i>
        <span>{{toastMsg}}</span>
    </div>
</template>

<script>
/* 
  封装提示框
  1 搭建基本样式布局 (Toast.vue)
  2 将显示隐藏以及需要灵活设置的信息 (图标,提示信息) 存储在了vuex
  3 增加了定时器实现提示框自动关闭 vue-actios
  4 给组件增加过渡效果 transition组件
*/
import {mapState} from 'vuex'
export default {
    computed: {
      ...mapState ({
        // 变量名:state => .模块.xxx
        toastMsg:(state)=> state.showToast.toastMsg,
        toastType:(state)=> state.showToast.toastType,

      }),
      getClass () {
        // 根据type 返回class名称
        if(this.toastType=="success") {
          return "iconfont icon-toast_chenggong"
        }else if(this.toastType=="danger") {
          return "iconfont icon-toast-shibai_huaban"
        }else if(this.toastType=="warning") {
          return "iconfont icon-toast-jinggao"
        }
      }
    }
}
</script>

<style lang = "less" scoped>
.toast{
  position: fixed;
  padding: 10px 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #fff;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  border-radius: 10px;

  .iconfont{
    margin-right: 10px;
  }

  .icon-toast-shibai_huaban{
    color: red;
  }

  .icon-toast_chenggong{
    color: green;
  }

  .icon-toast-jinggao{
    color: orange;
  }
}


</style>